﻿using furion.blog.Application.Contracts;
using furion.blog.Domain.IRepositories;
using furion.blog.Domain.MySQL;
using furion.blog.Domain.Query.Message;
using furion.blog.Domain.Shared;
using furion.blog.Domain.Shared.Dto.Message;
using furion.blog.Utils;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using Mapster;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.Application
{
    public class MessageService : IMessageService, ISingleton
    {

        private readonly IMessageRepository _messageRepository;
        private readonly IMemberRepository _memberRepository;
        public MessageService(IMessageRepository messageRepository,IMemberRepository memberRepository) {
            this._messageRepository = messageRepository;
            this._memberRepository = memberRepository;
        }

        /// <summary>
        /// 统计
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<long> Count(MessageDetailQuery query) {
            return await this._messageRepository.Count(query);
        }

        /// <summary>
        /// 当前登录会员信息列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageVO> MyList(MessageListQuery query) {
            var ret = await this._messageRepository.List(query);
            if (ret.Total > 0)
            {
                DataTable dt = (DataTable)ret.Records;
                foreach (DataRow dr in dt.Rows) {
                    if (dr["fromUser"].ToInt() == -1) {
                        //来自系统
                        dr["fromUserNickName"] = "系统";
                        dr["fromUserAvatar"] = "/images/ai.png";
                    }
                    if (dr["toUser"].ToInt() == -1) {
                        //系统接收
                        dr["toUserNickName"] = "系统";
                        dr["toUserAvatar"] = "/images/ai.png";
                    }
                }
            }
            return ret;
        }

        /// <summary>
        /// 信息分页
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageVO> List(MessageListQuery query) {
            var ret = await this._messageRepository.List(query);
            if (ret.Total > 0)
            {
                DataTable dt = (DataTable)ret.Records;
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["fromUser"].ToInt() == -1)
                    {
                        //来自系统
                        dr["fromUserNickName"] = "系统";
                        dr["fromUserAvatar"] = "/images/ai.png";
                    }
                    if (dr["toUser"].ToInt() == -1)
                    {
                        //系统接收
                        dr["toUserNickName"] = "系统";
                        dr["toUserAvatar"] = "/images/ai.png";
                    }
                }
            }
            return ret;
        }

        /// <summary>
        /// 设置信息为打开
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="memberId"></param>
        /// <returns></returns>
        public async Task<bool> Open(long messageId, long memberId) {
            return await this._messageRepository.Open(messageId,memberId);
        }


        /// <summary>
        /// 回复
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> Repeat(MessageRepeatDto dto) {
            //查询接收人是否存在
            if (dto.ToUser != -1) {
                Member member = await this._memberRepository.FindById((long)dto.ToUser);
                if (member == null)
                {
                    throw Oops.Oh("接收人不存在");
                }
            }
            Message model = null;
            model = dto.Adapt<Message>();
            model.MessageId = IdUtil.Id();
            return await this._messageRepository.Save(model);
        }


        /// <summary>
        /// 删除自己的信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="memberId"></param>
        /// <returns></returns>
        public async Task<bool> DelBySelf(long id, long memberId) {
            return await this._messageRepository.Del(id,memberId);
        }

    }
}
